/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean;

import Merlion.SCM.bean.InventoryItemsBeanRemote;
import Merlion.SCM.bean.InventoryMovementsManagementBeanRemote;
import Merlion.util.state.InventoryState;
import Merlion.util.state.WarehouseInventoryState;
import Merlion.util.state.WarehouseState;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import org.primefaces.event.RowEditEvent;


@ManagedBean(name = "WarehouseManagedBean")
@ViewScoped
public class WarehouseManagedBean implements Serializable {

    private WarehouseState newWarehouse = new WarehouseState();
    private List<WarehouseState> warehouses;//return list of warehouse
    private WarehouseState[] selectedWarehouses;
    private WarehouseState selectedWarehouse;
    private WarehouseState selectedViewWarehouse;
    private boolean editMode;
    private List<InventoryState> inventoryList;
    private Long warehouseId;
    private String inventoryType;
    private List<InventoryState> unhousedInventoryList;
    private InventoryState selectedUnhousedInventory;
    private String remarks;
    @EJB
    private InventoryItemsBeanRemote iib;
    @EJB
    private InventoryMovementsManagementBeanRemote wmb;

    public WarehouseManagedBean() {
    }

    public List<InventoryState> getInventoryList() {
        return inventoryList;
    }

    public void setInventoryList(List<InventoryState> inventoryList) {
        this.inventoryList = inventoryList;
    }

    public String getInventoryType() {
        return inventoryType;
    }

    public void setInventoryType(String inventoryType) {
        this.inventoryType = inventoryType;
    }

    public Long getWarehouseId() {
        return warehouseId;
    }

    public void setWarehouseId(Long warehouseId) {
        System.out.println("Set warehouse Id ");
        this.warehouseId = warehouseId;
    }

    public void handleInventoryTypeChange() {
        List<InventoryState> productList = new ArrayList<InventoryState>();
        List<InventoryState> materialList = new ArrayList<InventoryState>();
        System.out.println("Handle Inventory Type Change");
        if (selectedViewWarehouse != null) {
            WarehouseInventoryState wis = wmb.viewWarehouseInventory(selectedViewWarehouse.getId());
            productList = wis.getProducts();
            materialList = wis.getMaterials();
        }

        if (inventoryType.equals("P")) {
            this.setInventoryList(productList);
        } else {
            this.setInventoryList(materialList);
        }
    }

    public void initView(PhaseEvent event) {
        FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        if (warehouses == null) {
            warehouses = iib.viewWarehouse();
        }
    }

    public WarehouseState getNewWarehouse() {
        return newWarehouse;
    }

    public void setNewWarehouse(WarehouseState newWarehouse) {
        this.newWarehouse = newWarehouse;
    }

    public WarehouseState getSelectedViewWarehouse() {
        return selectedViewWarehouse;
    }

    public void setSelectedViewWarehouse(WarehouseState selectedViewWarehouse) {
        System.out.println("Set Selected View Warehouse");
        this.selectedViewWarehouse = selectedViewWarehouse;
    }

    public boolean isEditMode() {
        return editMode;
    }

    public void setEditMode(boolean editMode) {
        this.editMode = editMode;
    }

    public void addWarehouse(ActionEvent event) {
        System.out.println(":::WEB::: Add new warehouse");

        String name = newWarehouse.getName();
        int capacity = newWarehouse.getCapacity();
        String street = newWarehouse.getStreet();
        String city = newWarehouse.getCity();
        String country = newWarehouse.getCountry();
        String postcode = newWarehouse.getPostcode();

        Long newWarehouseId = iib.addWarehouse(name, capacity, street, city, country, postcode);

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("warehouses.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    public List<WarehouseState> getWarehouses() {
        if (warehouses == null) {
            warehouses = iib.viewWarehouse();
        }
        return this.warehouses;
    }

    public WarehouseState getSelectedWarehouse() {
        return selectedWarehouse;
    }

    public void setSelectedWarehouse(WarehouseState selectedWarehouse) {
        System.out.println(":::WEB::: Set Selected Warehouse");
        this.selectedWarehouse = selectedWarehouse;

    }

    public void onEditRowWarehouse(RowEditEvent event) {

        System.out.println(":::WEB::: On Edit Row");
        WarehouseState eWarehouse = (WarehouseState) event.getObject();

        if (eWarehouse != null) {
            System.out.println(":::WEB::: Edited warehouse not null");
            String eName = eWarehouse.getName();
            String eStreet = eWarehouse.getStreet();
            String eCity = eWarehouse.getCity();
            String eCountry = eWarehouse.getCountry();
            String ePostcode = eWarehouse.getPostcode();
            Long eWarehouseId = eWarehouse.getId();

            System.out.println(":::WEB::: Calling Edit Warehouse");
            iib.editWarehouse(eWarehouseId, eName, eStreet, eCity, eCountry, ePostcode);
        }

    }

    public void deleteWarehouse(ActionEvent Event) {

        System.out.println(":::WEB::: Delete Warehouse");

        if (selectedWarehouse != null) {
            System.out.println(":::WEB::: Selected warehouse not null");
            Long warehouseId = selectedWarehouse.getId();
            System.out.println(":::WEB::: Warehouse ID is : " + warehouseId);
            iib.deleteWarehouse(warehouseId);
            System.out.println(":::WEB::: Deletion done");
        } else {
            System.out.println(":::WEB::: Selected warehouse is null");
        }

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("warehouses.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    public WarehouseState[] getSelectedWarehouses() {
        return selectedWarehouses;
    }

    public void setSelectedWarehouses(WarehouseState[] selectedWarehouses) {
        System.out.println(":::WEB::: Set selected warehouses");
        this.selectedWarehouses = selectedWarehouses;
    }

    public void deleteWarehouses() {
        System.out.println(":::WEB::: Delete Warehouses");

        if (selectedWarehouses != null) {
            System.out.println(":::WEB::: If selected warehouses not null");
            for (int i = 0; i < selectedWarehouses.length; i++) {
                System.out.println("selectedWarehouse i: " + i + " - " + selectedWarehouses[i].getId());
                iib.deleteWarehouse(selectedWarehouses[i].getId());
            }
        } else {
            System.out.println(":::WEB::: Selected warehouses are null");
        }

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("warehouses.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    public void cancelDelete() {
        for (WarehouseState warehouse : selectedWarehouses) {
            warehouse = null;
        }
        if (selectedWarehouse == null) {
            System.out.println(":::WEB::: Deletion of warehouses cancelled");

        } else {
            System.out.println(":::WEB::: Deletion of warehouses are not cancelled");
        }

    }

    public void editWarehouse(ActionEvent event) {


        System.out.println(":::WEB::: On Edit Row");


        if (selectedViewWarehouse != null) {
            System.out.println(":::WEB::: Edited warehouse not null");
            String name = selectedViewWarehouse.getName();
            String street = selectedViewWarehouse.getStreet();
            String city = selectedViewWarehouse.getCity();
            String country = selectedViewWarehouse.getCountry();
            String postcode = selectedViewWarehouse.getPostcode();
            Long eWarehouseId = selectedViewWarehouse.getId();

            System.out.println(":::WEB::: Calling Edit Warehouse");
            iib.editWarehouse(eWarehouseId, name, street, city, country, postcode);
        }

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("warehouses.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }


    }

    public InventoryState getSelectedUnhousedInventory() {
        return selectedUnhousedInventory;
    }

    public void setSelectedUnhousedInventory(InventoryState selectedUnhousedInventory) {
        System.out.println("Set selected unhoused inventory");
        this.selectedUnhousedInventory = selectedUnhousedInventory;
    }

    public List<InventoryState> getUnhousedInventoryList() {
        if (unhousedInventoryList == null) {
            unhousedInventoryList = wmb.viewUnhousedInventory();
        }
        return unhousedInventoryList;
    }

    public void setUnhousedInventoryList(List<InventoryState> unhousedInventoryList) {
        System.out.println("Set Unhoused inventory");
        this.unhousedInventoryList = unhousedInventoryList;
    }

    public void assignInventory(ActionEvent event) {
        System.out.println("Assign Inventory");
        if (selectedUnhousedInventory != null) {
            System.out.println("Selected unhoused inventory not null!");
            System.out.println("Batch Id " + selectedUnhousedInventory.getBatchId() + "\n"
                    + "Warehouse Id = " + warehouseId);
            wmb.addInventoryBatchToWarehouse(selectedUnhousedInventory.getBatchId(), warehouseId);

        }
        
        
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("warehouses.xhtml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        
    }

    public void initViewforUnhousedInventories(PhaseEvent event) {
        if (unhousedInventoryList == null) {
            unhousedInventoryList = wmb.viewUnhousedInventory();
        }
    }

    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

    public void discardInventoryBatch(ActionEvent event) {
        System.out.println("Discard inventory batch from warehouse");
        if (selectedUnhousedInventory != null) {
            if (selectedUnhousedInventory.getType().equals("RAW")) {
                System.out.println("Discard material batch");
                try {
                    wmb.discardMaterialBatchFromWarehouse(selectedUnhousedInventory.getBatchId(),
                            selectedViewWarehouse.getId(), remarks);
                    try {
                        FacesContext.getCurrentInstance().getExternalContext().redirect("warehouses.xhtml");
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                } catch (IllegalStateException e) {
                    String msg = e.getMessage();
                    System.out.println(msg);
                    FacesContext context = FacesContext.getCurrentInstance();

                    context.addMessage(null, new FacesMessage("Discard failed!", msg));
                }
            } else {
                System.out.println("Discard finished product batch");
                try {
                    wmb.discardProductBatchFromWarehouse(selectedUnhousedInventory.getBatchId(),
                            selectedViewWarehouse.getId(), remarks);
                    try {
                        FacesContext.getCurrentInstance().getExternalContext().redirect("warehouses.xhtml");
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                } catch (IllegalStateException e) {
                    String msg = e.getMessage();
                    System.out.println(msg);
                    FacesContext context = FacesContext.getCurrentInstance();

                    context.addMessage(null, new FacesMessage("Discard failed!", msg));

                }
            }

        }
    }
}
    
